package com.school.demo.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.demo.model.auto.Activities;
import com.school.demo.model.auto.Organization;
import com.school.demo.service.IActivitiesService;
import com.school.demo.service.IOrganizationService;
import com.school.demo.util.Restful;
import com.school.demo.util.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: demo
 * @description: 活动控制类
 * @author: Charlotte
 * @create: 2021-03-26 10:39
 **/
@Api(tags = "活动管理API")
@RequestMapping("/api/activity")
@Controller
public class ActivitiesApiController {

    @Autowired
    IActivitiesService activitiesService;
    @Autowired
    IOrganizationService organizationService;

    /**
     * 获取活动列表
     */
    @ApiOperation(value = "分页获取活动", notes = "", tags = {})
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public @ResponseBody
    Restful list(@RequestParam(name = "schoolId", defaultValue = "1") Integer schoolId, @RequestParam(name = "offset", defaultValue = "1") Integer offset, @RequestParam(name = "limmit", defaultValue = "1") Integer limmit, @RequestParam(name = "tag", defaultValue = "") String tag) throws Exception {

        QueryWrapper<Organization> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Organization::getSchoolId, schoolId);
        List<Organization> list = organizationService.list(queryWrapper);
        List<Integer> ids = null;
        if (list == null) {
            return new Restful(Status.REQUEST_PARAMETER_ERROR, "schoolId错误或者学校下没有组织");
        }
        if (offset == null || offset == null) {
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }
        try {
            ids = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                ids.add(list.get(i).getId());
            }
            QueryWrapper<Activities> queryWrapper1 = new QueryWrapper();

            //使用find_in_set判断参数是否在数组中
            queryWrapper1.lambda().in(Activities::getOriganizationId, ids)
                    .apply(!tag.isEmpty(), "FIND_IN_SET ('" + tag + "',label)");

            Page<Activities> page = new Page<>(offset, limmit);
            Page<Activities> data = activitiesService.page(page, queryWrapper1);
            Restful restful = new Restful(Status.SUCCESS);
//            List<Activities> list1 = data.getRecords();
//
//            for (int i = 0; i < list1.size(); i++) {
//                Organization organization = null;
//                organization = organizationService.getById(list1.get(i).getOriganizationId());
//                if (null != organization){
//                    list1.get(i).setOrganization(organization);
//                }
//            }
            restful.setData(data);
            return restful;
        } catch (Exception e) {
            return new Restful(Status.WARN, e.getMessage());
        }
    }


    /**
     * 根据组织id，创建空活动，返回活动id
     */
    @ApiOperation(value = "根据组织id，创建空活动，返回活动id", notes = "", tags = {})
    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public @ResponseBody
    Restful newActivity(@RequestParam(name = "id") Integer id) throws Exception {
        if (id == null) {
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }
        Organization organization = organizationService.getById(id);
        if (organization == null) {
            return new Restful(Status.WARN, "organization不存在");
        }
        try {
            Activities newOne = new Activities();
            newOne.setOriganizationId(id);
            newOne.setStatus(0);

            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
            sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记
            newOne.setUpTime(sdf.format(now));
            Boolean success = activitiesService.save(newOne);
            if (!success) {
                return new Restful(Status.WARN);
            }
            Restful restful = new Restful(Status.SUCCESS);
            restful.setData(newOne);
            return restful;
        } catch (Exception e) {
            return new Restful(Status.WARN, e.getMessage());
        }

    }


    /**
     * 添加活动
     */
    @ApiOperation(value = "添加活动", notes = "", tags = {})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public @ResponseBody
    Restful save(@RequestBody Activities activities) throws Exception {

        if (activities == null) {
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }

        if (activities.getId() == null){
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }
        try {
            Boolean success = activitiesService.save(activities);
            if (!success) {
                return new Restful(Status.WARN);
            }
            Restful restful = new Restful(Status.SUCCESS);
            restful.setData(activities.getId());
            return restful;
        } catch (Exception e) {
            return new Restful(Status.WARN, e.getMessage());
        }

    }

}
